<?php
	
	//add by lin
	
	define('__ROOT__', dirname(dirname(__FILE__)));
	require_once(__ROOT__.'\models\Conn.php'); 
	require_once(__ROOT__.'\models\Resources.php');
	require_once(__ROOT__.'\PageView.php');
	//end add
	
class PerformanceController extends Controller{

	private $id;
	private $type;

	public function base(){
		
	}

	public function showEconomyList(){		
		
		//add by lin		
		$r1 = new Resources();		
		$economy_list = $r1->listAll();
		//end add;
		
		//获取过滤条件
		$select_area = $_POST['select_area'];//var_dump($select_area);die;
		$select_time = $_POST['select_time'];
		$select_indicator = $_POST['select_indicator'];
		if(empty($select_area)){
			$select_area = 'all';
		}
		if(empty($select_time)){
			$select_time = 'all';
		}
		if(empty($select_indicator)){
			$select_indicator = 'all';
		}
		//将过滤条件保存到session中
		$_SESSION['filter_condition']['select_area'] = $select_area;
		$_SESSION['filter_condition']['select_time'] = $select_time;
		$_SESSION['filter_condition']['select_indicator'] = $select_indicator;
		
		//获取选中的页码
		$pageNo = $_POST['pageNo'];
		
		//根据选择条件过滤$economy_list，并讲新的list存入$filter_economy_list
		$filter_economy_list = array();
		foreach($economy_list as $economy_item)
		{
			if(($economy_item['area'] == $select_area || $select_area=='all')
				&& ($economy_item['time'] == $select_time || $select_time=='all')
				&& ($economy_item['indicator'] == $select_indicator || $select_indicator=='all'))
			{
				array_push($filter_economy_list,$economy_item);
			}
		}
		
		//分页
		$page_size = 15;
		$page_count = ceil(count($filter_economy_list)/$page_size);	
		//$pageNo = $this->id; //获取当前页码
		if(empty($pageNo)){ 
			$pageNo = 1;
		}
		if(count($filter_economy_list) > $page_size*$pageNo){
			$pageData = array_slice($filter_economy_list, $page_size*($pageNo-1), $page_size);
		}else{
			$pageData = array_slice($filter_economy_list, $page_size*($pageNo-1));
		}
		
		//利用session传递参数
		//session_start();
		$_SESSION['economy_list'] = $economy_list;
		$_SESSION['page_para']['count'] = count($filter_economy_list);
		$_SESSION['page_para']['page_size'] = $page_size;
		$_SESSION['page_para']['pageNo'] = $pageNo;
		$_SESSION['page_para']['pageData'] = $pageData;
		header("Location:app/views/economy.php");
	}
	
	public function editPage()
	{
		$id = $this->id;
		$type = $this->type;
		//var_dump($type);die;
		//根据这个id值从数据库里获取相应的数据记录，这边暂时假设根据这个id获得到了数据，如下
		//$item = array("id"=>"1","area"=>"杨浦区","time"=>"2012","indicator"=>"人均GDP增长率","value"=>"123456","p5"=>"xxx");

		//add by lin
		$r1 = new Resources();
		$item = $r1->getById($id);
		
		print_r($item);
		
		$arg = implode(":",$item);
		header("Location:app/views/edit_performance.php?item=$arg&type=$type");
	}
	
	public function deleteItem()
	{
		$id = $this->id;
		$type = $this->type;
		//根据这个id值从数据库里删除一条记录
		//add by lin
		
		$r1 = new Resources();
		$r1->delete($id);
		//end add
		
		//var_dump($id);die;
		if($type == "economy")
		{
			$this->showEconomyList();
		}
		if($type == "ecology")
		{
			$this->showEcologyList();
		}
		if($type == "society")
		{
			$this->showSocietyList();
		}
	}
	
	public function addItemPage()
	{
		$type = $this->type;//var_dump($type);die;
		header("Location:app/views/add_performance.php?type=$type");
	}
	
	public function addItem()
	{
		$new_item_area = $_POST['area'];
		$new_item_time = $_POST['time'];
		$new_item_indicator = $_POST['indicator'];
		$new_item_value = $_POST['value'];

		$item = array("area"=>$new_item_area,"time"=>$new_item_time,"indicator"=>$new_item_indicator,"value"=>$new_item_value);
		
		//add by lin
		$r1 = new Resources();
		$r1->insert($item);
		
		$type = $this->type;
		if($type == "economy")
		{
			$this->showEconomyList();
		}
		if($type == "ecology")
		{
			$this->showEcologyList();
		}
		if($type == "society")
		{
			$this->showSocietyList();
		}
		
	}
	
	public function showEcologyList(){
		
		//从数据库中获取社会子系统的所有数据
		$resources = new Resources();
		$ecology_list = $resources->listAllEcology();
		
		//获取过滤条件
		$select_area = $_POST['select_area'];//var_dump($select_area);die;
		$select_time = $_POST['select_time'];
		$select_indicator = $_POST['select_indicator'];
		if(empty($select_area)){
			$select_area = 'all';
		}
		if(empty($select_time)){
			$select_time = 'all';
		}
		if(empty($select_indicator)){
			$select_indicator = 'all';
		}
		//将过滤条件保存到session中
		$_SESSION['filter_condition']['select_area'] = $select_area;
		$_SESSION['filter_condition']['select_time'] = $select_time;
		$_SESSION['filter_condition']['select_indicator'] = $select_indicator;
		
		//获取选中的页码
		$pageNo = $_POST['pageNo'];
		
		//根据选择条件过滤$economy_list，并讲新的list存入$filter_economy_list
		$filter_ecology_list = array();
		foreach($ecology_list as $ecology_item)
		{
			if(($ecology_item['area'] == $select_area || $select_area=='all')
			&& ($ecology_item['time'] == $select_time || $select_time=='all')
			&& ($ecology_item['indicator'] == $select_indicator || $select_indicator=='all'))
			{
				array_push($filter_ecology_list,$ecology_item);
			}
		}
		
		//分页
		$page_size = 15;
		$page_count = ceil(count($filter_ecology_list)/$page_size);
		//$pageNo = $this->id; //获取当前页码
		if(empty($pageNo)){
			$pageNo = 1;
		}
		if(count($filter_ecology_list) > $page_size*$pageNo){
			$pageData = array_slice($filter_ecology_list, $page_size*($pageNo-1), $page_size);
		}else{
			$pageData = array_slice($filter_ecology_list, $page_size*($pageNo-1));
		}
		
		//利用session传递参数
		//session_start();
		$_SESSION['ecology_list'] = $ecology_list;
		$_SESSION['page_para']['count'] = count($filter_ecology_list);
		$_SESSION['page_para']['page_size'] = $page_size;
		$_SESSION['page_para']['pageNo'] = $pageNo;
		$_SESSION['page_para']['pageData'] = $pageData;
		
		header("Location:app/views/ecology.php");
	}
	
	public function showSocietyList(){
	
		//从数据库中获取社会子系统的所有数据
		$resources = new Resources();
		$society_list = $resources->listAllSociety();
		
		//获取过滤条件
		$select_area = $_POST['select_area'];//var_dump($select_area);die;
		$select_time = $_POST['select_time'];
		$select_indicator = $_POST['select_indicator'];
		if(empty($select_area)){
			$select_area = 'all';
		}
		if(empty($select_time)){
			$select_time = 'all';
		}
		if(empty($select_indicator)){
			$select_indicator = 'all';
		}
		//将过滤条件保存到session中
		$_SESSION['filter_condition']['select_area'] = $select_area;
		$_SESSION['filter_condition']['select_time'] = $select_time;
		$_SESSION['filter_condition']['select_indicator'] = $select_indicator;
		
		//获取选中的页码
		$pageNo = $_POST['pageNo'];
		
		//根据选择条件过滤$economy_list，并讲新的list存入$filter_economy_list
		$filter_society_list = array();
		foreach($society_list as $society_item)
		{
			if(($society_item['area'] == $select_area || $select_area=='all')
			&& ($society_item['time'] == $select_time || $select_time=='all')
			&& ($society_item['indicator'] == $select_indicator || $select_indicator=='all'))
			{
				array_push($filter_society_list,$society_item);
			}
		}
		
		//分页
		$page_size = 15;
		$page_count = ceil(count($filter_society_list)/$page_size);
		//$pageNo = $this->id; //获取当前页码
		if(empty($pageNo)){
			$pageNo = 1;
		}
		if(count($filter_society_list) > $page_size*$pageNo){
			$pageData = array_slice($filter_society_list, $page_size*($pageNo-1), $page_size);
		}else{
			$pageData = array_slice($filter_society_list, $page_size*($pageNo-1));
		}
		
		//利用session传递参数
		//session_start();
		$_SESSION['society_list'] = $society_list;
		$_SESSION['page_para']['count'] = count($filter_society_list);
		$_SESSION['page_para']['page_size'] = $page_size;
		$_SESSION['page_para']['pageNo'] = $pageNo;
		$_SESSION['page_para']['pageData'] = $pageData;

		header("Location:app/views/society.php");
	}

	public function setParams($params)
	{
		$this->id = $params[0];
		$this->type = $params[1];
	}
}
